<?PHP

// This class is used as a wrapper for users
class folio_user {

    // Identifying information, uniquely ids any pieces of content, mapping back into Elgg.
    public $ident = -1;
    public $name = '';
    public $username = '';
    public $password = '';
    public $email = '';
    public $icon = -1;
    public $active = '';
    public $alias = '';
    public $code = '';
    public $file_quota = -1;
    public $template_id = -1;
    public $owner = -1;
    public $user_type = '';
    public $moderation = '';
    public $last_action = '';
    public $template_name = '';

    // Constructor
    function folio_user( $array = false ) {
        // Assume constructor is an array from the db.
        if ( !!$array ) {
            foreach ( $array as $n=>$v ) {
                $this->$n = $v;
            }
        }
    }

//////////////////
//              //
//  SQL Layer   //
//              //
//////////////////

    // Return the user for the passed ident.
    function SelectWhereIdent( $ident ) {
        global $CFG;

        $ident = intval( $ident );

        $query = "SELECT u.ident, u.* FROM {$CFG->prefix}users u " .
                    "WHERE u.ident = $ident";

        $records = recordset_to_array( get_recordset_sql( $query ));

        if( !$records ) {return false; }

        return new folio_user( $records[$ident] );
    }

    // Return the user for the passed ident.
    function SelectWhereUsername( $username ) {
        global $CFG;

        $username = folio_clean( $username );

        $query = "SELECT * FROM {$CFG->prefix}users " .
                    "WHERE username = '$username'";

        $records = recordset_to_array( get_recordset_sql( $query ));

        if( !$records ) {return false; }

        foreach( $records as $record ) {
            return new folio_user( $record );
        }
    }

    // Called to update an user in the db.
    function Update( ) {

        return ( self::local_update_record( 'users', $this, 'ident' ) );
    }

    // This is a modification of update_record in datalib.php that is
    //      able to accept alternative ident column names.
    private function local_update_record($table, $dataobject, $ident) {
        global $db, $CFG;

        static $table_columns;

        // Determine all the fields in the table
        if (is_array($table_columns) && array_key_exists($table,$table_columns)) {
            $columns = $table_columns[$table];
        } else {
            if (!$columns = $db->MetaColumns($CFG->prefix . $table)) {
                return false;
            }
            $table_columns[$table] = $columns;
        }

        $data = (array)$dataobject;
        $ddd = array();

        if (defined('ELGG_PERFDB')) { global $PERF ; $PERF->dbqueries++; };

        // Pull out data matching these fields
        foreach ($columns as $column) {
            if ($column->name <> $ident and isset($data[$column->name]) ) {
                $ddd[$column->name] = $data[$column->name];
            }
        }

        // Construct SQL queries
        $numddd = count($ddd);
        $count = 0;
        $update = '';

        foreach ($ddd as $key => $value) {
            $count++;
            $update .= $key .' = ?';
            if ($count < $numddd) {
                $update .= ', ';
            }
        }

//return( 'UPDATE '. $CFG->prefix . $table .' SET '. $update .' WHERE ' . $ident . ' = \''. $dataobject->$ident .'\'' );
        $stmt = $db->Prepare('UPDATE '. $CFG->prefix . $table .' SET '. $update .' WHERE ' . $ident . ' = \''. $dataobject->$ident .'\'');
        if ($rs = $db->Execute($stmt,$ddd)) {
            elggcache_delete($table, "{$ident}_" . $dataobject->$ident);
            return true;
        } else {
            if (isset($CFG->debug) and $CFG->debug > 7) {
                notify($db->ErrorMsg() .'<br /><br />UPDATE '. $CFG->prefix . $table .' SET '. $update .' WHERE ' . $ident . ' = \''. $dataobject->$ident .'\'');
            }
            if (!empty($CFG->dblogerror)) {
                $debug = debug_backtrace();
                foreach ($debug as $d) {
                    if (strpos($d['file'],'datalib') === false) {
                        error_log("SQL ".$db->ErrorMsg()." in {$d['file']} on line {$d['line']}. STATEMENT:  UPDATE $CFG->prefix $table SET $update WHERE $ident = '{$dataobject->$ident}'");
                        break;
                    }
                }
            }
            return false;
        }
    }

}
?>
